package com.cloudbroker.bcs.platform.ltb.middle.service.impl;

import com.cloudbroker.bcs.common.dao.base.DAOBase;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.VerifyFlagStatus;
import com.cloudbroker.bcs.platform.ltb.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.ltb.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.ltb.common.util.SessionHelper;
import com.cloudbroker.bcs.platform.ltb.middle.dao.ValidateCodeDAO;
import com.cloudbroker.bcs.platform.ltb.middle.entity.ValidateCode;
import com.cloudbroker.bcs.platform.ltb.middle.service.ValidateCodeService;
import com.cloudbroker.bcs.platform.ltb.middle.service.base.TenantServiceImplBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by tianrui on 2016/7/5.
 */

@Service
public class ValidateCodeServiceImpl extends TenantServiceImplBase<ValidateCode, String> implements
        ValidateCodeService {

    @Resource
    private ValidateCodeDAO validate_codeDAO;

    @Override
    protected DAOBase<ValidateCode, String> getDAO() {
        return validate_codeDAO;
    }

    private ValidateCode getLastCode(String mobile_tel) {
        return validate_codeDAO.getLastCode(mobile_tel);
    }

    /**
     * 校验验证码次数
     *
     * @param verify_source
     * @param mobile_tel
     * @return
     */
    @Override
    public boolean checkCodeTime(String mobile_tel, String broker_key, Integer verify_source) {

        int checkTime = ConfigValues.getVerifyCodeTimesInOutTime();

        //有时间改为缓存查询count
        SessionHelper sessionHelper = SessionHelper.getInstance();
        CacheHelper cacheHelper = CacheHelper.getInstance();
        String dailySendVerifySourceKey = sessionHelper.getDailySendVerifySourceKey(mobile_tel + broker_key,
                ValueUtil.getString(verify_source));
        int sendVerifyCount = ValueUtil.getInt(cacheHelper.get(dailySendVerifySourceKey), 0);
        if (sendVerifyCount >= checkTime) {
            //验证码数据库还未实现
//            ValidateCode code = this.getLastCode(login_account);
//            if (code != null) {
//                code.setValidate_status(VerifyFlagStatus.FAILURE.getVal());
//                this.update(code);
//            }
            return false;
        }
        return true;
    }

    @Override
    public void addValidateCode(ValidateCode validateCode) {
        super.save(validateCode);
    }


    /**
     * 校验验证码
     *
     * @param check_code
     * @param mobile_tel
     * @param broker_key
     * @return
     */
    @Override
    public boolean checkCode(String check_code, String mobile_tel, String broker_key, Integer verify_source) {
        if (isSuperVerifyCodePassed(check_code)) {
            return true;
        }
        Integer checkStatus = 0;
        SessionHelper sessionHelper = SessionHelper.getInstance();
        CacheHelper cacheHelper = CacheHelper.getInstance();
        String mobileVerifyKey = sessionHelper.getMobileVerifyKey(mobile_tel, ValueUtil.getString(verify_source));
        boolean mobileVerifyKeyExist = cacheHelper.exists(mobileVerifyKey);
        if (mobileVerifyKeyExist && cacheHelper.get(mobileVerifyKey).equals(check_code)) {
            checkStatus = VerifyFlagStatus.PASSED.getVal();
        } else {
            checkStatus = VerifyFlagStatus.FAILURE.getVal();

            //验证码校验失败,如果mobileVerifyKey在缓存中存在,则未修改入数据库,数据库需要进行失败修改,并从缓存中移除该key
            String dailySendVerifySourceKey = sessionHelper.getDailySendVerifySourceKey(mobile_tel + broker_key,
                    ValueUtil.getString(verify_source));
            int sendVerifyCount = ValueUtil.getInt(cacheHelper.get(dailySendVerifySourceKey), 0);
            int outTime = ConfigValues.getMobileVerifyCodeTimeout();
            sessionHelper.setInfoForSession(dailySendVerifySourceKey, sendVerifyCount + 1 + "", outTime);

            String sendVerifyKey24Hour = sessionHelper.getSendVerifyKeyConfig(mobile_tel,
                    ValueUtil.getString(verify_source));
            sessionHelper.setInfoForSession(sendVerifyKey24Hour, System.currentTimeMillis() + "", outTime);
        }
        if (checkStatus == VerifyFlagStatus.PASSED.getVal()) {
            ValidateCode code = this.getLastCode(mobile_tel);
            if (code != null) {
                code.setValidate_status(checkStatus);
                this.update(code);
                return true;
            }
        }
        return false;
    }

    /**
     * 是否超级验证码
     *
     * @param auth_check_code
     * @return
     */
    private boolean isSuperVerifyCodePassed(String auth_check_code) {
        return ConfigValues.getVerifyCodeSuperSwitch()
                && ConfigValues.getVerifyCodeSuperValue().equalsIgnoreCase(auth_check_code);
    }
}
